<template>
  <a-card class="problem-card">
    <template v-if="!problemLoaded">
      <!-- Skeleton loading state -->
      <a-skeleton active :paragraph="{ rows: 10 }" />
    </template>
    <template v-else>
      <div class="problem-header">
        <div class="problem-title">
          <span class="problem-id">{{ problem.problemId }}</span>
          <h1>{{ problem.title }}</h1>
          <a-tag :color="getDifficultyColor(problem.difficulty)">{{ problem.difficulty }}</a-tag>
        </div>
      </div>
      <div class="problem-content">
        <div class="section">
          <h2>描述</h2>
          <div class="content" v-html="problem.description"></div>
        </div>
        <div class="section">
          <h2>输入</h2>
          <div class="content" v-html="problem.input"></div>
        </div>
        <div class="section">
          <h2>输出</h2>
          <div class="content" v-html="problem.output"></div>
        </div>
        <div class="section">
          <h2>示例</h2>
          <div v-for="(example, index) in problem.exampleData" :key="index" class="example">
            <div class="example-header">示例 {{ index + 1 }}</div>
            <div class="example-content">
              <div class="example-input">
                <div class="example-label">输入:</div>
                <pre>{{ example.input }}</pre>
              </div>
              <div class="example-output">
                <div class="example-label">输出:</div>
                <pre>{{ example.output }}</pre>
              </div>
            </div>
          </div>
        </div>
      </div>
    </template>
  </a-card>
</template>

<script>
import { computed } from 'vue';

export default {
  name: 'ProblemDetail',
  props: {
    problem: {
      type: Object,
      required: true
    }
  },
  setup(props) {
    // 检查 problem 是否已加载
    const problemLoaded = computed(() => {
      return props.problem && Object.keys(props.problem).length > 0;
    });

    // 根据难度返回不同的颜色
    const getDifficultyColor = (difficulty) => {
      switch (difficulty) {
        case '简单':
          return 'success';
        case '中等':
          return 'warning';
        case '困难':
          return 'error';
        default:
          return 'default';
      }
    };

    return {
      problemLoaded,
      getDifficultyColor
    };
  }
};
</script>

<style scoped>
.problem-card {
  height: 100%;
  overflow-y: auto;
  border-radius: 8px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.problem-header {
  margin-bottom: 24px;
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 16px;
}

.problem-title {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
}

.problem-id {
  font-size: 16px;
  color: #8c8c8c;
}

.problem-title h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
}

.problem-content {
  font-size: 14px;
}

.section {
  margin-bottom: 24px;
}

.section h2 {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 12px;
  color: #262626;
}

.content {
  color: #595959;
  line-height: 1.6;
}

.example {
  margin-bottom: 16px;
  border: 1px solid #f0f0f0;
  border-radius: 4px;
  overflow: hidden;
}

.example-header {
  background: #fafafa;
  padding: 8px 12px;
  font-weight: 500;
  border-bottom: 1px solid #f0f0f0;
}

.example-content {
  padding: 12px;
}

.example-input, .example-output {
  margin-bottom: 12px;
}

.example-label {
  font-weight: 500;
  margin-bottom: 4px;
  color: #262626;
}

.example-content pre {
  background: #f5f5f5;
  padding: 12px;
  border-radius: 4px;
  margin: 0;
  white-space: pre-wrap;
  word-break: break-all;
  font-family: monospace;
}

/* 响应式样式 */
@media (max-width: 768px) {
  .problem-title {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
}
</style>